home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / dialgmgr.sit / Dialogs ƒ / RadioGroup.p < prev    next >
Text File  |  1990-01-04  |  6KB  |  239 lines

  1. Unit TRadioGroup;
  2.  
  3. {        ⌐1986-1989            Bill Stackhouse                                    }
  4. {                                Stackhouse Software                                }
  5. {                                Natick, MA 01760                                    }
  6.  
  7. Interface
  8.  
  9. {$IFC UNDEFINED UseRadioGroup}
  10. {$SETC UseRadioGroup = FALSE}
  11. {$ENDC}
  12. {$IFC UseRadioGroup}
  13.  
  14.     Uses
  15.         TObject;
  16.  
  17.     Const
  18.         maxRadios = 15;
  19.  
  20.     Type
  21.         RadioSet = Packed Set Of 0..255;
  22.  
  23.         TRadioGroup = Object(TObject)
  24.                 numRadGroups: 0..maxRadios;                {number of radio groups}
  25.                 radSet: Array[1..maxRadios] Of Record
  26.                         singleButton: Boolean;                    {TRUE if only one button in group}
  27.                         nowOn: Integer;                            {currently on}
  28.                         btns: RadioSet;                            {buttons in radio group}
  29.                         title: String;                                {title of group}
  30.                         titleTop: Boolean;                            {TRUE - title on top, FALSE - title at left end}
  31.                         groupBox: Rect;                            {box around group}
  32.                     End;
  33.                 Procedure TRadioGroup.Init;
  34.                 Procedure TRadioGroup.Add (pOn: Integer;    {which is initially on}
  35.                                             pBtns: RadioSet;                            {which btns in a single group}
  36.                                             pTitle: String;                            {if length > 0, then title with box}
  37.                                             pTitleTop: Boolean);                        {TRUE - title on top, FALSE - title at left end}
  38.                 Procedure TRadioGroup.Revise (curDialog: DialogPtr);
  39.                 Procedure TRadioGroup.Draw (i: Integer);
  40.                 Procedure TRadioGroup.DrawAll;
  41.                 Procedure TRadioGroup.Click (curDialog: dialogPtr;
  42.                                             theItem: Integer);
  43.                 Procedure TRadioGroup.Get (theGroup: Integer;
  44.                                             Var on: Integer);
  45.                 Function TRadioGroup.Error: Integer;
  46.             End;
  47.  
  48. {$ENDC}
  49.  
  50. Implementation
  51.  
  52. {$IFC UseRadioGroup}
  53.  
  54.     Const
  55.         Off = 0;
  56.         On = 1;
  57.  
  58.         btnCtrlItem = 4;
  59.         chkCtrlItem = 5;
  60.         radCtrlItem = 6;
  61.         editCtrlItem = 16;
  62.  
  63.         DialogGroupIgnored = -10;    {too many groups, key, menus, or user items were added}
  64.  
  65.     Type
  66.         TSearch = (searching, found, endList);
  67.  
  68.     Var
  69.         globalError: Integer;
  70.  
  71.     Procedure TRadioGroup.Init;
  72.     Begin
  73.         globalError := noErr;
  74.         SELF.numRadGroups := 0;
  75.     End;        {TRadioGroup.Init}
  76.  
  77.     Procedure TRadioGroup.Add (pOn: Integer;    {which is initially on}
  78.                                     pBtns: RadioSet;                            {which btns in a single group}
  79.                                     pTitle: String;                            {if length > 0, then title with box}
  80.                                     pTitleTop: Boolean);                        {TRUE - title on top, FALSE - title at left end}
  81.         Var
  82.             count: Integer;
  83.             i: Integer;
  84.     Begin
  85.         globalError := noErr;
  86.         With SELF Do
  87.             If numRadGroups < maxRadios Then
  88.                 Begin
  89.                     numRadGroups := numRadGroups + 1;
  90.                     With radSet[numRadGroups] Do
  91.                         Begin
  92.                             count := 0;
  93.                             For i := 1 To 255 Do
  94.                                 If i In pBtns Then
  95.                                     count := count + 1;
  96.                             singleButton := (count = 1);
  97.                             nowOn := pOn;
  98.                             btns := pBtns;
  99.                             title := pTitle;
  100.                             titleTop := pTitleTop;
  101.                         End;
  102.                 End
  103.             Else
  104.                 globalError := DialogGroupIgnored;
  105.     End;        {TRadioGroup.Add}
  106.  
  107.     Procedure TRadioGroup.Revise (curDialog: DialogPtr);
  108.         Var
  109.             i, j: Integer;
  110.             theType: Integer;
  111.             theHandle: Handle;
  112.             theBox: Rect;
  113.     Begin
  114. {Set default radio buttons and draw title with box}
  115.         globalError := noErr;
  116.         With SELF Do
  117.             For i := 1 To numRadGroups Do    {initialize default radio buttons}
  118.                 With radSet[i] Do
  119.                     Begin
  120.                         If nowOn > 0 Then
  121.                             SELF.Click(curDialog, nowOn);
  122.                         SetRect(groupBox, 0, 0, 0, 0);
  123.                         If Length(title) > 0 Then
  124.                             Begin
  125.                                 For j := 1 To 50 Do
  126.                                     If j In btns Then
  127.                                         Begin
  128.                                             GetDItem(curDialog, j, theType, theHandle, theBox);
  129.                                             If (groupBox.left = 0) Or (theBox.left < groupBox.left) Then
  130.                                                 groupBox.left := theBox.left;
  131.                                             If (groupBox.right = 0) Or (theBox.right > groupBox.right) Then
  132.                                                 groupBox.right := theBox.right;
  133.                                             If (groupBox.top = 0) Or (theBox.top < groupBox.top) Then
  134.                                                 groupBox.top := theBox.top;
  135.                                             If (groupBox.bottom = 0) Or (theBox.bottom > groupBox.bottom) Then
  136.                                                 groupBox.bottom := theBox.bottom;
  137.                                         End;    {in btns}
  138.                             End;    {if title}
  139.                     End;    {with}
  140.     End;        {TRadioGroup.Revise}
  141.  
  142.     Procedure TRadioGroup.Draw (i: Integer);
  143.         Var
  144.             theBox: Rect;
  145.             titleBox: Rect;
  146.     Begin
  147.         globalError := noErr;
  148.         With SELF.radSet[i] Do
  149.             If Length(title) > 0 Then
  150.                 Begin
  151.                     theBox := groupBox;
  152.                     If titleTop Then
  153.                         Begin        {at the top}
  154.                             InsetRect(theBox, -3, -2);
  155.                             theBox.top := theBox.top - 6;
  156.                             FrameRect(theBox);
  157.                             titleBox.left := theBox.left + 3;
  158.                             titleBox.bottom := theBox.top + 4;
  159.                             titleBox.right := titleBox.left + StringWidth(title) + 2;
  160.                             titleBox.top := titleBox.bottom - 14;
  161.                             EraseRect(titleBox);
  162.                             MoveTo(theBox.left + 5, theBox.top + 4);
  163.                             DrawString(title);
  164.                         End
  165.                     Else
  166.                         Begin        {at the side}
  167.                             InsetRect(theBox, -3, -1);
  168.                             FrameRect(theBox);
  169.                             MoveTo(theBox.left - StringWidth(title) - 3, theBox.bottom - 5);
  170.                             DrawString(title);
  171.                         End;
  172.                 End;
  173.     End;        {TRadioGroup.Draw}
  174.  
  175.     Procedure TRadioGroup.DrawAll;
  176.         Var
  177.             i: Integer;
  178.     Begin
  179.         globalError := noErr;
  180.         For i := 1 To SELF.numRadGroups Do
  181.             SELF.Draw(i);
  182.     End;        {TRadioGroup.DrawAll}
  183.  
  184.     Procedure TRadioGroup.Click (curDialog: dialogPtr;
  185.                                     theItem: Integer);
  186.         Var
  187.             newType, oldType: Integer;
  188.             newHandle, oldHandle: handle;
  189.             theBox: Rect;
  190.             index: Integer;
  191.             state: TSearch;
  192.     Begin
  193.         globalError := noErr;
  194.         GetDItem(curDialog, theItem, newType, newHandle, theBox);
  195.         If newType = radCtrlItem Then
  196.             Begin
  197.                 state := searching;
  198.                 index := 1;
  199.                 Repeat
  200.                     If (theItem In SELF.radSet[index].btns) Then
  201.                         state := found
  202.                     Else If index > SELF.numRadGroups Then
  203.                         state := endList
  204.                     Else
  205.                         index := index + 1;
  206.                 Until (state <> searching);
  207.                 If state = found Then
  208.                     Begin
  209.                         If SELF.radSet[index].singleButton Then {only one button in group}
  210.                             SetCtlValue(ControlHandle(newHandle), BitXor(GetCtlValue(ControlHandle(newHandle)), 1))
  211.                         Else
  212.                             Begin
  213.                                 GetDItem(curDialog, SELF.radSet[index].nowOn, oldType, oldHandle, theBox);
  214.                                 If oldType = radCtrlItem Then
  215.                                     SetCtlValue(ControlHandle(oldHandle), Off);
  216.                                 SetCtlValue(ControlHandle(newHandle), On);
  217.                                 SELF.radSet[index].nowOn := theItem;
  218.                             End;
  219.                     End    {if found}
  220.                 Else
  221.                     SetCtlValue(ControlHandle(newHandle), On);
  222.             End;    {if radio item}
  223.     End;        {TRadioGroup.Key}
  224.  
  225.     Procedure TRadioGroup.Get (theGroup: Integer;
  226.                                     Var on: Integer);
  227.     Begin
  228.         globalError := noErr;
  229.         on := SELF.radSet[theGroup].nowOn;
  230.     End;        {TRadioGroup.GetNowOn}
  231.  
  232.     Function TRadioGroup.Error: Integer;
  233.     Begin
  234.         Error := globalError;
  235.     End;        {TRadioGroup.Error}
  236.  
  237. {$ENDC}
  238.  
  239. End.